﻿using FastExecutor.Base.Design;
using FastExecutor.Base.Util;
using FastExecutor.ORG.Entity;
using FastIOC.Annotation;
using FastORM.Applicaiton;
using FastUtil.Base;
using FastUtil.Extra;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace FastExecutor.ORG.DB
{
    public class DB_Table : FastTemplateDB<Frame_Table>
    {
        [AutoWired]
        private DB_Column @DB_Column;
        [AutoWired]
        private TemplateUtil @TemplateUtil;
        public override void Insert(Frame_Table Entity)
        {
            if (Entity.RowGuid == null)
            {
                Entity.RowGuid = CommonUtil.CreateCommonGuid();
            }
            Entity.AddDate = DateTime.Now;
            base.Insert(Entity);
            string DDLSQL = string.Format("CREATE TABLE {0} (RowGuid VARCHAR(100), UGuid VARCHAR(100),UName VARCHAR(100),DGuid VARCHAR(100),DName VARCHAR(100),AddDate datetime, OrderNum int)", Entity.SQLTableName);
            @DBUtil.ExecuteSQL(DDLSQL);
            Frame_TableStruct Column = new Frame_TableStruct();
            Column.RowGuid = CommonUtil.CreateCommonGuid();
            Column.FieldName = "主键";
            Column.SQLFieldName = "RowGuid";
            Column.FieldLength = 100;
            Column.FieldType = "nvarchar";
            Column.OrderNum = 999;
            Column.TableGuid = Entity.RowGuid;
            Column.AddDate = DateTime.Now;
            Column.IsSys = 1;
            Column.Control = "input";
            Column.BindCodeGuid = "";
            @DBUtil.Insert(Column);
            Column = new Frame_TableStruct();
            Column.RowGuid = CommonUtil.CreateCommonGuid();
            Column.FieldName = "人员主键";
            Column.SQLFieldName = "UGuid";
            Column.FieldLength = 100;
            Column.FieldType = "nvarchar";
            Column.OrderNum = 998;
            Column.TableGuid = Entity.RowGuid;
            Column.AddDate = DateTime.Now;
            Column.IsSys = 1;
            Column.Control = "input";
            Column.BindCodeGuid = "";
            @DBUtil.Insert(Column);
            Column = new Frame_TableStruct();
            Column.RowGuid = CommonUtil.CreateCommonGuid();
            Column.FieldName = "人员姓名";
            Column.SQLFieldName = "UName";
            Column.FieldLength = 100;
            Column.FieldType = "nvarchar";
            Column.OrderNum = 997;
            Column.TableGuid = Entity.RowGuid;
            Column.AddDate = DateTime.Now;
            Column.IsSys = 1;
            Column.Control = "input";
            Column.BindCodeGuid = "";
            @DBUtil.Insert(Column);
            Column = new Frame_TableStruct();
            Column.RowGuid = CommonUtil.CreateCommonGuid();
            Column.FieldName = "部门主键";
            Column.SQLFieldName = "DGuid";
            Column.FieldLength = 100;
            Column.FieldType = "nvarchar";
            Column.OrderNum = 996;
            Column.TableGuid = Entity.RowGuid;
            Column.AddDate = DateTime.Now;
            Column.IsSys = 1;
            Column.Control = "input";
            Column.BindCodeGuid = "";
            @DBUtil.Insert(Column);
            Column = new Frame_TableStruct();
            Column.RowGuid = CommonUtil.CreateCommonGuid();
            Column.FieldName = "部门名称";
            Column.SQLFieldName = "DName";
            Column.FieldLength = 100;
            Column.FieldType = "nvarchar";
            Column.OrderNum = 995;
            Column.TableGuid = Entity.RowGuid;
            Column.AddDate = DateTime.Now;
            Column.IsSys = 1;
            Column.Control = "input";
            Column.BindCodeGuid = "";
            @DBUtil.Insert(Column);
            Column = new Frame_TableStruct();
            Column.RowGuid = CommonUtil.CreateCommonGuid();
            Column.FieldName = "添加时间";
            Column.SQLFieldName = "AddDate";
            Column.FieldLength = 0;
            Column.FieldType = "datetime";
            Column.OrderNum = 994;
            Column.TableGuid = Entity.RowGuid;
            Column.AddDate = DateTime.Now;
            Column.IsSys = 1;
            Column.Control = "input";
            Column.BindCodeGuid = "";
            @DBUtil.Insert(Column);
            Column = new Frame_TableStruct();
            Column.RowGuid = CommonUtil.CreateCommonGuid();
            Column.FieldName = "排序";
            Column.SQLFieldName = "OrderNum";
            Column.OrderNum = 993;
            Column.FieldLength = 11;
            Column.FieldType = "int";
            Column.TableGuid = Entity.RowGuid;
            Column.AddDate = DateTime.Now;
            Column.IsSys = 1;
            Column.Control = "input";
            Column.BindCodeGuid = "";
            @DBUtil.Insert(Column);
        }

        public override void BatchDelete(List<string> KeyGuidList)
        {
            foreach (string KeyGuid in KeyGuidList)
            {
                Frame_Table Entity = FindOne(KeyGuid);
                string DDLSQL = string.Format("DROP Table {0}", Entity.SQLTableName);
                @DBUtil.ExecuteSQL(DDLSQL);
                @DBUtil.ExecuteSQL(string.Format("delete from Frame_TableStruct where TableGuid='{0}'", KeyGuid));
            }
            base.BatchDelete(KeyGuidList);
        }

        public string ExportXML(List<string> KeyGuidList)
        {
            XmlDocument Doc = new XmlDocument();
            //创建根节点    
            XmlNode Root = Doc.CreateElement("TableInfos");
            foreach (string KeyGuid in KeyGuidList)
            {
                Frame_Table Entity = FindOne(KeyGuid);
                List<Frame_TableStruct> ColumnEntityList = @DB_Column.FindAll(string.Format(" and TableGuid='{0}'", KeyGuid));
                XmlNode TableInfo = Doc.CreateElement("TableInfo");
                XmlNode TableDetail = Doc.CreateElement("TableDetail");
                TableDetail.InnerXml = ConvertUtil.ConvertEnity2InnerXml(Entity);
                TableInfo.AppendChild(TableDetail);
                XmlNode TableColumns = Doc.CreateElement("Columns");
                foreach (Frame_TableStruct Item in ColumnEntityList)
                {
                    XmlNode Column = Doc.CreateElement("Column");
                    Column.InnerXml = ConvertUtil.ConvertEnity2InnerXml(Item);
                    TableColumns.AppendChild(Column);
                }
                TableInfo.AppendChild(TableColumns);
                Root.AppendChild(TableInfo);
            }
            Doc.AppendChild(Root);
            return ConvertUtil.ConvertXml2String(Doc);
        }

        public string CreateCodeFile(string Prefix, string NameSpace, List<string> KeyGuidList)
        {
            string FileID = "CodeGenerate" + CommonUtil.CreateCommonGuid();
            Dictionary<string, object> CodeDic = new Dictionary<string, object>();
            string[] NameSpaceArray = NameSpace.Split(new char[] { '.' }, StringSplitOptions.RemoveEmptyEntries);
            string[] PageColumnFilter = new string[] { "RowGuid", "UGuid", "UName", "DGuid", "DName", "AddDate" };
            foreach (string KeyGuid in KeyGuidList)
            {
                Frame_Table Table = FindOne(KeyGuid);
                 string FileTableName = string.Empty;
                if (!string.IsNullOrEmpty(Prefix))
                {
                     FileTableName = Table.SQLTableName.Replace(Prefix, string.Empty);
                }
                List<Frame_TableStruct> ColumnList = @DB_Column.FindAll(string.Format(" and TableGuid='{0}'", KeyGuid), "OrderNum desc,AddDate");
                CodeDic.Clear();
                CodeDic.Add("VueIntro", "el: '#app'");
                CodeDic.Add("NameSpance", NameSpace);
                CodeDic.Add("EntityName", Table.SQLTableName);
                CodeDic.Add("RoutePrefix", FileTableName);
                CodeDic.Add("FieldList", ColumnList);
                CodeDic.Add("AreaName", NameSpaceArray[NameSpaceArray.Length - 1]);
                @TemplateUtil.GeneratemeplateFile(FileID, Table.SQLTableName, "Entity.vm", Table.SQLTableName + ".cs", CodeDic);
                @TemplateUtil.GeneratemeplateFile(FileID, Table.SQLTableName, "DB.vm", "DB_" + FileTableName + ".cs", CodeDic);
                @TemplateUtil.GeneratemeplateFile(FileID, Table.SQLTableName, "Controller.vm", FileTableName + "Controller" + ".cs", CodeDic);
                ColumnList = ColumnList.Where(a => !PageColumnFilter.Contains(a.SQLFieldName)).ToList();
                CodeDic.Clear();
                CodeDic.Add("VueIntro", "el: '#app'");
                CodeDic.Add("NameSpance", NameSpace);
                CodeDic.Add("EntityName", FileTableName);
                CodeDic.Add("FieldList", ColumnList);
                CodeDic.Add("AreaName", NameSpaceArray[NameSpaceArray.Length - 1]);
                @TemplateUtil.GeneratemeplateFile(FileID, Table.SQLTableName, "ListPage.vm", FileTableName + "_List" + ".cshtml", CodeDic);
                @TemplateUtil.GeneratemeplateFile(FileID, Table.SQLTableName, "AddPage.vm", FileTableName + "_Add" + ".cshtml", CodeDic);
                @TemplateUtil.GeneratemeplateFile(FileID, Table.SQLTableName, "EditPage.vm", FileTableName + "_Edit" + ".cshtml", CodeDic);
            }
            string CodePath = System.Web.HttpContext.Current.Server.MapPath("~/" + TemplateUtil.CodeTempPath + "/" + FileID);
            ZipUtil.CompressDirectory(CodePath, System.Web.HttpContext.Current.Server.MapPath("~/" + @TemplateUtil.CodeTempPath + "/" + FileID + ".zip"), 5, true);
            return FileID;
        }
    }
}
